package com.example.pickcolor;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private TextView pick;
	private Spinner choose;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		choose .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String selectedText = arg0.getItemAtPosition(arg2).toString();
				Log.i ("SPINNER", selectedText);
				ViewGroup layout1 = (ViewGroup) findViewById( R.id.layout1 );
				if(selectedText.equals("⿊黑⾊色 | Black")){
					pick.setTextColor(Color.BLACK);
					layout1.setBackgroundColor(Color.GREEN);
				}else if(selectedText.equals("綠⾊色 | Green")){
					pick.setTextColor(Color.GREEN);
					layout1.setBackgroundColor(Color. RED);
				}else{
					pick.setTextColor(Color.GRAY );
					layout1.setBackgroundColor(Color.WHITE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		pick = (TextView) findViewById(R.id.textView1) ;
		choose = (Spinner) findViewById(R.id.spinner1);
		
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.colors, android.R.layout.simple_spinner_item);
				// 設置下拉樣式
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				// 為下拉清單設置配接器
				choose.setAdapter(adapter);
				}


	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		String ItemText = item.getTitle().toString();
		Toast.makeText(this, ItemText, Toast.LENGTH_SHORT).show();
		return super.onMenuItemSelected(featureId, item);
	}

}
